package objectdiagrameditor.features.object;

import org.eclipse.graphiti.features.IAddFeature;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.impl.AbstractAddFeature;
import org.eclipse.graphiti.mm.algorithms.Polyline;
import org.eclipse.graphiti.mm.algorithms.RoundedRectangle;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.algorithms.styles.Orientation;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.IGaService;
import org.eclipse.graphiti.services.IPeCreateService;
import org.eclipse.graphiti.util.ColorConstant;
import org.eclipse.graphiti.util.IColorConstant;

public class AddObjectFeature extends AbstractAddFeature implements
		IAddFeature {
	
	private static final IColorConstant CLASS_TEXT_FOREGROUND = new ColorConstant(51, 51, 153);
	private static final IColorConstant CLASS_FOREGROUND = new ColorConstant(200, 102, 0);
	private static final IColorConstant CLASS_BACKGROUND = new ColorConstant(200, 204, 153);

	public AddObjectFeature(IFeatureProvider fp) {
		super(fp);
	}

	@Override
	public boolean canAdd(IAddContext context) {
		// TODO: check for right domain object instance below
		return  context.getNewObject() instanceof fUML.Semantics.Classes.Kernel.Object &&  context.getTargetContainer() instanceof Diagram;
	}

	@Override
	public PictogramElement add(IAddContext context) {

		fUML.Semantics.Classes.Kernel.Object addedObject = (fUML.Semantics.Classes.Kernel.Object) context.getNewObject();
		//here we use type object since the canadd method already ensures that a package is the targetcontainer
		Object targetDiagram = context.getTargetContainer();
		//Diagram targetDiagram = (Diagram) context.getTargetContainer();			
		// CONTAINER SHAPE WITH ROUNDED RECTANGLE
        IPeCreateService peCreateService = Graphiti.getPeCreateService();
        ContainerShape containerShape = peCreateService.createContainerShape((ContainerShape)targetDiagram, true);
        // define a default size for the shape
        int width = 120;
        int height = 80; 
        
        IGaService gaService = Graphiti.getGaService();
        
        {
            // create and set graphics algorithm
            RoundedRectangle roundedRectangle = gaService.createRoundedRectangle(containerShape, 5, 5);
            roundedRectangle.setForeground(manageColor(CLASS_FOREGROUND));
            roundedRectangle.setBackground(manageColor(CLASS_BACKGROUND));
            roundedRectangle.setLineWidth(2);
            gaService.setLocationAndSize(roundedRectangle,context.getX(), context.getY(), width, height);
            // if added Object has no resource we add it to the resource 
            // of the diagram
            if (addedObject.eResource() == null) {
            		getDiagram().eResource().getContents().add(addedObject);
            }
        }
        // SHAPE WITH LINE
        {
            // create shape for line
            Shape shape = peCreateService.createShape(containerShape, false);
            // create and set graphics algorithm
            Polyline polyline = gaService.createPolyline(shape, new int[] { 0, 20, width, 20 });
            polyline.setForeground(manageColor(CLASS_FOREGROUND));
            polyline.setLineWidth(2);
        }
        // SHAPE WITH TEXT
        {
            // create shape for text
            Shape shape = peCreateService.createShape(containerShape, false);
            // create and set text graphics algorithm
            Text text = gaService.createDefaultText(getDiagram(),shape, addedObject.getClass().getName());
            text.setForeground(manageColor(CLASS_TEXT_FOREGROUND));
            text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
            text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
            gaService.setLocationAndSize(text, 0, 0, width, 20);
            
            // provide information to support direct-editing directly 
            // after object creation (must be activated additionally)
           // IDirectEditingInfo directEditingInfo = getFeatureProvider().getDirectEditingInfo();
            // set container shape for direct editing after object creation
            //directEditingInfo.setMainPictogramElement(containerShape);
            // set shape and graphics algorithm where the editor for
            // direct editing shall be opened after object creation
            //directEditingInfo.setPictogramElement(shape);
            //directEditingInfo.setGraphicsAlgorithm(text);
        }
      
		peCreateService.createChopboxAnchor(containerShape);
		
		// TODO: enable the link to the domain object
		
		
		//!!!!!!!!!!!!!!!!!!!
		 link(containerShape, addedObject);

		return containerShape;
	}
}
